package com.jyxd.web.controller.basic;

import com.jyxd.web.data.basic.ComNumber;
import com.jyxd.web.service.assessment.basic.ComNumberService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/comNumber")
public class ComNumberController {

    private static Logger logger= LoggerFactory.getLogger(ComNumberController.class);

    @Autowired
    private ComNumberService comNumberService;

    /**
     * 根据条件分页查询串口记录列表（也可以不分页）
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryList",method= RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","暂无数据");
        if(map!=null && map.containsKey("start")){
            int totalCount =comNumberService.queryNum(map);
            map.put("start",((int)map.get("start")-1)*(int)map.get("size"));
            json.put("totalCount",totalCount);
        }
        List<ComNumber> list =comNumberService.queryList(map);
        if(list!=null && list.size()>0){
            json.put("msg","查询成功");
            json.put("data",JSONArray.fromObject(list));
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
    /**
     * 增加一条串口记录
     *
     * @return
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public String insert(@RequestBody ComNumber comNumber) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "添加失败");
        comNumber.setId(UUIDUtil.getUUID());
        comNumber.setCreateTime(new Date());
        comNumberService.insert(comNumber);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "添加成功");
        return json.toString();
    }

    /**
     * 更新串口的床号和呼吸机编号
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "更新失败");
        if (map != null && map.containsKey("comNumberId")) {
            ComNumber comNumber = comNumberService.queryData(map.get("comNumberId").toString());
            if(map.get("bedCode")!=null && !"".equals(map.get("bedCode").toString())){
                comNumber.setBedCode(map.get("bedCode").toString());
            }
            if(map.get("respiratorNumber")!=null && !"".equals(map.get("respiratorNumber").toString())){
                comNumber.setRespiratorNumber(map.get("respiratorNumber").toString());
            }
            comNumber.setStatus(1);
            comNumber.setCreateTime(new Date());
            comNumberService.update(comNumber);
            json.put("msg", "更新成功");
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }
    /**
     * 根据主键id查询串口记录
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        if (map != null && map.containsKey("id")) {
            ComNumber comNumber  = comNumberService.queryData(map.get("id").toString());
            if (comNumber != null) {
                json.put("msg", "查询成功");
                json.put("data", JSONObject.fromObject(comNumber));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 清空床号和呼吸机型号
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/claerBedCodeAndRespiratorNumber")
    @ResponseBody
    public String claerBedCodeAndRespiratorNumber(@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("msg", "更新失败");
        if (map != null && map.containsKey("id")) {
            ComNumber comNumber = comNumberService.queryData(map.get("id").toString());
            comNumber.setBedCode(null);
            comNumber.setRespiratorNumber(null);
            comNumber.setStatus(1);
            comNumber.setCreateTime(new Date());
            comNumberService.update(comNumber);
            json.put("msg", "更新成功");
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
